using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;

public class HillClimbing : MonoBehaviour
{
    public Tilemap map;
    public Button btn;
    private GameObject player;
    private Vector3Int start;
    private Vector3Int end;

    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.Find("Player");
        btn.onClick.AddListener(foo);
    }

    void foo() 
    {
        start = new Vector3Int(37, -35, 0);
        end = new Vector3Int(37, -35, 0);
        if(start == end) {
            Debug.Log("Reachable.");
            return;
        }
        while(start != end) {
            ArrayList list = new ArrayList();
            Vector3Int next = start;
            double f = 999999;
            for(int i = -1; i <= 1; i++) {
                for(int j = -1; j <= 1; j++) {
                    Vector3Int p = new Vector3Int(start.x+i, start.y+j, 0);
                    if(map.HasTile(p)) {
                        list.Add(p);
                    }
                }
            }
            int offsetX = Math.Abs(end.x - start.x);
            int offsetY = Math.Abs(end.y - start.y);
            double square = offsetX * offsetX + offsetY * offsetY;
            double current_h = Math.Pow(square, 0.5);
            for(int i = 0; i < list.Count; i++) {
                Vector3Int p = (Vector3Int)list[i];
                offsetX = Math.Abs(start.x - p.x);
                offsetY = Math.Abs(start.y - p.y);
                square = offsetX * offsetX + offsetY * offsetY;
                double g = Math.Pow(square, 0.5);
                offsetX = Math.Abs(end.x - p.x);
                offsetY = Math.Abs(end.y - p.y);
                square = offsetX * offsetX + offsetY * offsetY;
                double h = Math.Pow(square, 0.5);
                if(current_h > h && f > g + h) {
                    next = p;
                    f = g + h;
                }
            }
            if(start == next) {
                if(start != end) {
                    Debug.Log("Unreachable.");
                }
                break;
            } else {
                start = next;
                if(start == end) {
                    Debug.Log("Reachable.");
                }
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
               
    }
}
